package fr.utbm.aiechat.activities;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import fr.utbm.aiechat.LocalisationData;
import fr.utbm.aiechat.Network;
import fr.utbm.aiechat.R;
import fr.utbm.aiechat.structures.User;

public class UserInfoActivity extends Activity
{
	private Bundle b;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        // Select our main view.
        setContentView(R.layout.userinfo_layout);
        
        TextView pseudoLabel= (TextView) findViewById(R.id.pseudoLabel);
        pseudoLabel.setText("Pseudo: ");
        
        TextView sexeLabel= (TextView) findViewById(R.id.sexeLabel);
        sexeLabel.setText("Sexe: ");
        
        TextView distanceLabel= (TextView) findViewById(R.id.distanceLabel);
        distanceLabel.setText("Distance: ");
        
        TextView pseudoValue= (TextView) findViewById(R.id.pseudoValue);
        TextView sexeValue= (TextView) findViewById(R.id.sexeValue);
        TextView distanceValue= (TextView) findViewById(R.id.distanceValue);
        
        b = getIntent().getExtras();
        int idUser = b.getInt("idUser");
        
        User user = Network.getInstance().Users.getItem(idUser);
        
        pseudoValue.setText(user.getName());
        sexeValue.setText(user.getSexe());
        
        if(LocalisationData.getInstance().getLat() != 0 && LocalisationData.getInstance().getLng() != 0 && user.getLat() != 0 && user.getLng() != 0){
        	double distance = distance(LocalisationData.getInstance().getLat(), LocalisationData.getInstance().getLng(), user.getLat(), user.getLng(), "K");
        	distanceValue.setText(distance+"km");
        }
        else{
        	distanceValue.setText("Impossible de calculer la distance");
        }
        
        //distanceValue.setText("lat:"+user.getLat()+"lng:"+user.getLng());
    }
	
	private double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
		  double theta = lon1 - lon2;
		  double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
		  dist = Math.acos(dist);
		  dist = rad2deg(dist);
		  dist = dist * 60 * 1.1515;
		  if (unit.equals("K")) {
		    dist = dist * 1.609344;
		  } else if (unit.equals("N")) {
		  	dist = dist * 0.8684;
		  }
		  return (dist);
		}

		/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
		/*::  This function converts decimal degrees to radians             :*/
		/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
		private double deg2rad(double deg) {
		  return (deg * Math.PI / 180.0);
		}

		/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
		/*::  This function converts radians to decimal degrees             :*/
		/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
		private double rad2deg(double rad) {
		  return (rad * 180.0 / Math.PI);
		}
}
